/**
 * 菜单管理extjs gbp 2018年1月23日 11:23:38
 */
var menuStore;
var iHBody;
var iWBody;
var projectName;
Ext.onReady(function(){
	iHBody=document.body.clientHeight||document.documentElement.clientHeight;//自适应高
	iWBody=document.body.clientWidth||document.documentElement.clientWidth;//自适应宽
	var pathName=window.document.location.pathname;
	projectName=pathName.substring(0,pathName.substr(1).indexOf('/')+1);//获取项目名
	
	var menuMode = Ext.define('menuModel',{
		extend:'Ext.data.Model',
		id:"menuModelId",
		fields:[
				{name:'mid',type:'String'},
				{name:'menuCode',type:'String'},
				{name:'text',type:'String'},
				{name:'leaf',type:'boolean'},
				{name:'url',type:'String'},
				{name:'iconCls',type:'String'},
				{name:'operate',type:'String'},
				{name:'operateTime',type:'String',convert:function(value){  
		            var createTime = Ext.Date.format(new Date(value),"Y-m-d H:i:s");
		            return createTime;  
		        }}
		       ]
	});
	
	menuStore = Ext.create('Ext.data.Store',{
		model:menuMode,
		autoLoad: true,
		pageSize: 17,  //页容量5条数据
		proxy:{
			type:'ajax',
			url:projectName+'/meanAction/showALLTreeMenu.do',
			reader:{
				type:'json',
				root: 'root',  //数据
				totalProperty: 'total'
			}
		},
		root:{
			expanded:true
		}
	});
	var tbar = Ext.create('Ext.toolbar.Toolbar',{
		width:40,
		items:[
				{
					xtype:'label',
					text:'菜单名称'
				},
				{
					xtype:'textfield',
					id:'menuName'
				},'-',
				{
					text:'<span style="color:white;font-size:300">查询</span>',
					style: 'background: #368ECE;border-color:#126DAF',
					icon: '../../images/minico/search.png',
					handler:function(){
						var menuName=Ext.getCmp('menuName').getValue();
						menuStore.load({params:{menuName:menuName,start: 0,limit: 17}});	
					}
				},'-',
				{
					text:'<span style="color:white;font-size:300">新增菜单</span>',
					style: 'background: #368ECE;border-color:#126DAF',
					icon: '../../images/minico/sign_add.png',
					handler:function(){
						var win = new Ext.create('Ext.window.Window',{
							id:"addNewMenu",
						    title:'新增菜单',       //弹出窗口内布局会充满整个窗口;
						    modal: true, //是否模态窗口，默认为false
						    width:380,          //设置窗口大小;
						    height:iHBody-200,
						    closeAction:'hide', //点击右上角关闭按钮后会执行的操作;
						   	closable:true,     //隐藏关闭按钮;
						    draggable:true,     //窗口可拖动;
						    resizable: false,
						    items:[newMenuTab]
						  });
						  win.show();
					}
				}]
	});
	new Ext.create('Ext.grid.Panel',{
		id:'mianTabId',
		store:menuStore,
		height:iHBody,
		columnLines: true,
		renderTo:Ext.getBody(),
		tbar:tbar,
		selModel: Ext.create("Ext.selection.CheckboxModel", {
		    injectCheckbox: 1,//checkbox位于哪一列，默认值为0
		    mode: "multi",//multi,simple,single；默认为多选multi
		    checkOnly: true,//如果值为true，则只用点击checkbox列才能选中此条记录
		    allowDeselect: true,//如果值true，并且mode值为单选（single）时，可以通过点击checkbox取消对其的选择
		    enableKeyNav: true
		}),
    	columns: [
    		{ header: '序号', xtype: 'rownumberer', width: '3%',align: 'center', sortable: false },
    		{ text: 'id',dataIndex:'mid',hidden:true},
    		{ text: '菜单名称', dataIndex: 'text',width:'10%',align: 'center', sortable: false },
	        { text: '菜单编码', dataIndex: 'menuCode',width:'10%',align: 'left',},
	        { text: '子节点',dataIndex:'leaf',align: 'center',width:'6%',
	        	renderer:function(value){
	        		if(value){
	                       return"<font color='red'><strong>是</strong></font>";
	                  }else{
	                	  return"<font color='green'>不是</font>";
	                  }
	        	}
	        },
	        { text: 'URL', dataIndex: 'url',align: 'left',width:'20%',sortable: false },
	        { text: '操作人', dataIndex: 'operate',align: 'center',width:'10%',sortable: false },
	        { text: '操作时间', dataIndex: 'operateTime',align: 'center',width:'10%',sortable: false },
	        {
	        	xtype:'gridcolumn',
	        	width:'10%',
	        	dataIndex: 'operate',
			    text: '操作栏',
			    align: 'center',
			    renderer:function(value, metaData, record){
			    	var uid = record.data.id;
			    	btnStr='<span>'+'<img src="../../images/minico/sign_cacel.png" alt="删除" onclick=\"deleteJiarInfo(\''+uid+'\');\"/>'+'&nbsp;&nbsp;'+
			    	'<img src="../../images/minico/editor.png" alt="修改" onclick=\"openmsgs(\''+uid+'\');\"/></span>';  
			    	return btnStr;
			    }
	        }
    	],
    	bbar: [
    		{
                xtype: 'pagingtoolbar',
                store: menuStore,
                displayMsg: '显示 {0} - {1} 条，共计 {2} 条',
                emptyMsg: "没有数据",
                beforePageText: "当前页",
                afterPageText: "共{0}页",
                displayInfo: true
           }],
	});
	
	//数据store
	var menuNameStore = Ext.create('Ext.data.Store',{
		fields: ["text", "menuCode"],
		autoLoad: true,
		proxy:{
			type:'ajax',
			url:projectName+'/meanAction/findFeatherMenu.do',
			reader:{
				type:'json'
			}
		},
		root:{
			expanded:true
		}
	});
	var isleafStore = Ext.create("Ext.data.Store", {
	    fields: ["isleafname", "isleafvalue"],
	    data: [
	        { isleafname: "有", isleafvalue: 1 },
	        { isleafname: "没有", isleafvalue: 0 }
	    ]
	});
	var jiedianStore = Ext.create("Ext.data.Store", {
	    fields: ["jiedianname", "jiedianvalue"],
	    data: [
	        { jiedianname: "是根菜单", jiedianvalue: "folder" },
	        { jiedianname: "不是根菜单", jiedianvalue: "elbow" }
	    ]
	});
	
	var newMenuTab = new Ext.FormPanel({
		bodyStyle:'margin-top:3px',
		layoyt:'form',
		border:false,
		height:iHBody-240,
		id:"newAddMenuFormID",
		items:[{
			xtype: "combobox",              //使用xtype定义
			name: "leaf",                 //form提交时的名称
			fieldLabel: "是否有子菜单",             //显示的Label
			id:'isleafGai',
			store: isleafStore,             //绑定的Store
			editable: false,                //是否可编辑
			displayField: "isleafname",           //显示的字段
			valueField: "isleafvalue",            //值的字段
			emptyText: "--请选择--",        //当没有值时的水印文字
			queryMode: "local",
			anchor:"90%",
			listeners: {
                change: function(view, record, item, index, e) {
               		if (record == '0') {//0 表示没有根节点、可以致父类为不可用
               			Ext.getCmp('fuleiNV').disable();
               			Ext.getCmp('newJdURL').disable();
               		} else {
               			Ext.getCmp('fuleiNV').enable();
               			Ext.getCmp('newJdURL').enable();
               		}
                }
                
            }	
		},{
			xtype: "combobox",              //使用xtype定义
			name: "fatherId",                 //form提交时的名称
			fieldLabel: "父菜单",             //显示的Label
			id:'fuleiNV',
			store: menuNameStore,             //绑定的Store
			editable: false,				//是否可编辑
			triggerAction : 'all', 
			displayField: "text",           //显示的字段
			valueField: "menuCode",            //值的字段
			emptyText: "--请选择--",        //当没有值时的水印文字
			queryMode: "local",              //查询模式
			anchor:"90%",
		},{
			xtype:'textfield',  
            fieldLabel:'子菜单', 
            id:'newJdName',
            name:'text',  
            width:'180',  
            allowBlank:'节点名不能为空',
            anchor:"90%",
		},
		{
			xtype: "combobox",              //使用xtype定义
			name: "iconCls",               //form提交时的名称
			id:'jiedianIcon',
			fieldLabel: "菜单图标",             //显示的Label
			store: jiedianStore,             //绑定的Store
			editable: false,                //是否可编辑
			displayField: "jiedianname",           //显示的字段
			valueField: "jiedianvalue",            //值的字段
			emptyText: "--请选择--",        //当没有值时的水印文字
			queryMode: "local" ,
			anchor:"90%",
			listeners: {
                change: function(view, record, item, index, e) {
               		if (record == 'folder') {//folder 表示根节点、
               			Ext.getCmp('newJdURL').disable();
               		} else {
               			Ext.getCmp('newJdURL').enable();
               		}
                }
                
            }	
		},
		{
			xtype:'textarea',  
            fieldLabel:'新增菜单URL',
            id:'newJdURL',
            name:'url',  
            width:'180',  
            allowBlank:'节点的URL',
            anchor:"90%",
		}],
		buttonAlign: 'center',
		buttons: [
        {
        	text:'<span style="color:white;font-size:300">保存</span>',
			style: 'background: #368ECE;border-color:#126DAF',
			icon: '../../images/minico/save.png',
            handler: function () {
            	var frm = Ext.getCmp("newAddMenuFormID");
            	if (!frm.getForm().isValid()){
            		return;
            	}
            	frm.getForm().submit({
            		url:projectName+'/meanAction/addMenuInfoToDb.do',
            		method:'post',
            		success: function(frm, action) {
			           Ext.Msg.alert('提示', '保存成功');
			           Ext.getCmp("addNewMenu").hide();
			           menuStore.load();
			         },
			        params:{
			        	fatherId:Ext.getCmp('fuleiNV').getValue()
			        },
			        failure: function(frm, action) {
			             Ext.Msg.alert('提示', '原因如下：' + action);
			        }
            		
            	});
            }
        }, {
        	text:'<span style="color:white;font-size:300">关闭</span>',
			style: 'background: #368ECE;border-color:#126DAF',
			icon: '../../images/minico/cancel.png',
            handler: function () {
            	Ext.getCmp("addNewMenu").hide();
            }
        }
      ]
	});
});

//删除菜单的方法
function deleteJiarInfo (value){
	Ext.MessageBox.confirm("提示", "你确定要删除此项？", function(btnId){
		if (btnId == "yes") {  
			Ext.Ajax.request({
				url:projectName+"/meanAction/deleteMeunInfoByid.do",
				mehtod:'get',
				params:{
					"menuId":value
				},
				success : function(response, options) {
					var result = JSON.parse(response.responseText);
					if (result.flag == "true") {
						Ext.Msg.alert('提示', '删除成功');
						menuStore.load();
					} 
					if (result.flag == "false") {
						Ext.Msg.alert('提示',result.msg);
					}
				},
				failure : function(response, options) {
					var result = JSON.parse(response.responseText);
					Ext.Msg.alert('提示', '原因如下：' + result);
				}
				
			});
		}else if (btnId == "no") {  
	       return;
	    }  
	});
}